package com.atguigu.yygh.hosp.controller.api;

import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.hosp.repository.DepartmentRepository;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.hosp.utils.HttpRequestHelper;
import com.atguigu.yygh.hosp.utils.MD5;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.Schedule;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

//平台对外提供接口
@Api(tags = "医院管理API接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    @ApiOperation(value = "获取排班分页列表")
    @PostMapping("schedule/list")
    public Result schedule(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String)paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String)paramMap.get("limit"));

        Page<Schedule> pages = scheduleService.selectPageSchedule(page,limit);
        return Result.ok(pages);
    }

    @ApiOperation(value = "上传排班")
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

        scheduleService.saveSchedule(paramMap);
        return Result.ok();
    }

    @ApiOperation(value = "删除科室")
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

        //获取医院编号和科室编号
        String hoscode = (String)paramMap.get("hoscode");
        String depcode = (String)paramMap.get("depcode");

        departmentService.removeDept(hoscode,depcode);
        return Result.ok();
    }

    @ApiOperation(value = "科室获取分页列表")
    @PostMapping("department/list")
    public Result department(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String)paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String)paramMap.get("limit"));

        //调用方法
        Page<Department> pages = departmentService.selectPageDept(page,limit);
        return Result.ok(pages);
    }

    @ApiOperation(value = "上传科室")
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        departmentService.saveDepartment(paramMap);
        return Result.ok();
    }

    @ApiOperation(value = "获取医院信息")
    @PostMapping("hospital/show")
    public Result hospital(HttpServletRequest request) {
        //获取医院系统传递数据
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);

        //获取医院编号
        String hoscode = (String)paramMap.get("hoscode");

        //调用方法
        Hospital hospital = hospitalService.findHospHoscode(hoscode);
        return Result.ok(hospital);
    }

    @ApiOperation(value = "上传医院")
    @PostMapping("saveHospital")
    public Result saveHospital(HttpServletRequest request) {
        //1 获取医院系统传递过来map集合数据
        //复选框 爱好
        //String hoscode = request.getParameter("hoscode");
        Map<String, String[]> parameterMap = request.getParameterMap();
        // Map<String, String[]>  -- Map<String, Object>
        Map<String, Object> paramMapString = HttpRequestHelper.switchMap(parameterMap);

        //签名校验
        //获取医院系统传递签名key,已经MD5加密
        String signHosp = (String)paramMapString.get("sign");

        //根据医院编号查询平台存储医院key，进行MD5加密
        String hoscode = (String)paramMapString.get("hoscode");
        String signKeyYygh = hospitalSetService.getSignKeyByHoscode(hoscode);
        //进行MD5加密
        //String encryptMD5 = MD5.encrypt(signKeyYygh);

        //比对
        if(!signHosp.equals(signKeyYygh)) {
            throw new YyghException(20001,"签名失败");
        }

        //base64传输过程中“+”转换为了“ ”，因此我们要转换回来
        String logoData = (String)paramMapString.get("logoData");
        logoData = logoData.replaceAll(" ","+");
        paramMapString.put("logoData",logoData);

        //2 把获取map集合数据添加mongodb
        hospitalService.saveHosp(paramMapString);
        return Result.ok();
    }
}
